﻿//===============================================================================
// File: AnswerGroup.cs
// Author:Veiller Hu
// CLR Version：4.0.30319.225
// Created Date:  2011/5/3 8:44:39 PM 
// Summary:  
//===============================================================================
//
// Copyright (c) East China Information Technology Corporation. All rights reserved.
//
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
// KIND, WHETHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
// PURPOSE.
//===============================================================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GPMS.BLL
{
    using GPMS.Model;
    public static class AnswerGroupHelper
    {

        public static List<AnswerGroupVM> GetList(ExtendedGPMSDb db,Guid userid)
        {
            var list= db.AnswerGroups
                .Where(a => a.Leader.Specialty.Users.Any(u => u.UserID == userid))
                .Select(a => new AnswerGroupVM() 
                { 
                    AnswerGroup=a,
                    Leader=a.Leader
                })
                .ToList();
            list.ForEach(i =>
                {
                   
                    i.Students = db.AnswerGroups.FirstOrDefault(a => a.AnswerGroupID == i.AnswerGroup.AnswerGroupID).Students.ToList();
                    i.Teachers = db.AnswerGroups.FirstOrDefault(a => a.AnswerGroupID == i.AnswerGroup.AnswerGroupID).Teachers.ToList();
                  
                });
            return list;
        }

        public static List<AnswerGroupVM> GetUserList(ExtendedGPMSDb db, Guid userid)
        {
            var list = db.AnswerGroups
                .Where(a => a.Leader.Specialty.Users.Any(u => u.UserID == userid) && 
                    (a.LeaderID==userid || a.Students.Any(s=>s.UserID==userid) || a.Teachers.Any(t=>t.UserID==userid)))
                .Select(a => new AnswerGroupVM()
                {
                    AnswerGroup = a,
                    Leader = a.Leader
                })
                .ToList();
            list.ForEach(i =>
            {
                i.SelectionID = new Dictionary<Guid, Guid>();
                i.Students = db.AnswerGroups.FirstOrDefault(a => a.AnswerGroupID == i.AnswerGroup.AnswerGroupID).Students.ToList();
                i.Teachers = db.AnswerGroups.FirstOrDefault(a => a.AnswerGroupID == i.AnswerGroup.AnswerGroupID).Teachers.ToList();
                i.Students.ForEach(s =>
                {
                    i.SelectionID.Add(s.UserID, db.GPSelections.FirstOrDefault(gps => gps.IsConfirm && gps.UserID == s.UserID).SelectionID);
                });
            });
            return list;
        }
        

        public static Guid Add(AnswerGroup model,ExtendedGPMSDb db)
        {
            if (model.AnswerGroupID==Guid.Empty || model.AnswerGroupID==null)
            {
                model.AnswerGroupID = Guid.NewGuid();
            }
            if (IsExist(model.Name,db))
            {
                throw new DuplicateException();
            }
            db.AnswerGroups.AddObject(model);
            db.SaveChanges();
            return model.AnswerGroupID;
        }


        public static bool IsExist(string name, ExtendedGPMSDb db)
        {
            return db.AnswerGroups.Any(a => a.Name == name);
        }

        public static void Delete(Guid id, ExtendedGPMSDb db)
        {
            AnswerGroup model = db.AnswerGroups.FirstOrDefault(a => a.AnswerGroupID == id);
            model.Students.Clear();
            model.Teachers.Clear();
            db.AnswerGroups.DeleteObject(model);
            db.SaveChanges();
        }
    }
}
